# -*- coding: utf-8 -*-
# @Time    : 2021/3/28 9:32
# @Author  : lilu
# @Email   : 1906196327@qq.com
# @File    : 正则1.py
# @Software: PyCharm

import  re

# with open("test",'r+',encoding='utf-8') as test:
#     for i in test:
#         ret = re.findall("^#|^%|^\$",i)
#         if not ret:
#             print(i)

# 正则重复
# 通配符
# \w字母数字下划线
# ？（匹配前一项0次或1次） *（匹配前一项0次或多次）  +
# ret = re.findall("\w?","python#!3")
# #匹配前面的单词数字只出现了一次，所有显示。(匹配一个或者0个字符）
# #特殊字符在/w中匹配不到，表示在问号中只匹配0次，所以显示，最后面的结尾没有字符，表示匹配0次，所以显示
# # 只匹配？前面一个字符进行匹配0次或者1次，如：py？是对y进行的匹配（出现一次或者零次）
# print(ret)
# ret = re.findall("\w*","python#!3")
# # 可以匹配很多个字符
# print(ret)


# ###### 正则分组
# # 用括号进行分组
# msg = "tel:130-4724-4030"
# ret = re.search(r'(\d{3})-(\d{4})-(\d{4})',msg)
# print(ret.group())
# print(ret.group(0))# 匹配全部
# print(ret.group(1))# 匹配第一个括号
# print(ret.group(2))# 匹配第二个括号
# print(ret.group(3))# 匹配第三个括号
# print(ret.groups())# 返回分组括号里的内容，加入到元组中
# #
#
# # findall 捕获分组
# 分组之后，匹配到的数据会放在内存中，斌且给一个我以下标为1开始的索引
# 所以捕获分组可以进行分组向后引用
# 只会匹配捕获分组的内容
# ret = re.findall(r'(\d{3})-(\d{4})-(\d{4})',msg)
# print(ret)


# # 分组向后引用
# # 前面分组匹配到的内容，在后面出现一摸一样的分组内容
# # 捕获到就放在内存里，打上标签
# str1 = "a1 d1 a1 d1"
# print(re.search(r"([a-z][0-9] [a-z][0-9])",str1).group())
# print(re.search(r"([a-z][0-9] [a-z][0-9]) \1",str1).group())


# # 非捕获分组
# # 不捕获，只分组，不放在内存，不向后引用，不会在内存中缓存分组匹配的值，括号里面之前加?:
# ret = re.search(r"(?:[a-z][0-9])\s+([a-z][0-9])",str1)
# ret = re.search(r"(?:[a-z][0-9])\s+([a-z][0-9])\s+\1",str1)# \1代表后面第一个捕获分组d1的捕获内容，此语句会出错， str1="a1 b1 b1“就会正确
# print(ret.group())
#
# # fandall 有捕获分组，只会打印捕获分组
# ret = re.findall(r"(?:[a-z][0-9])\s+([a-z][0-9])",str1)
# print(ret)

# msg = "weweyyy@xxx.com123@qq.comcomaaa@126.combbb@163.comcc@abc.com"
# ret = re.findall(r"(?:com)*([1-9a-z]{2,}@(?:123|qq|163|126)\.com)",msg)
# print(ret)
#
# msg2 = "<html>test1<div>test2</div><div><a>test3</a></div></html>"
# ret = re.findall(r"(?:test2|test3)",msg2)
# print(ret)

# ####### 命名分组
# msg = "hello 123 world 456"
# ret = re.search(r"(?P<sanle>\d{3})\s+(\w+)",msg)
# print(ret.group())
# print(ret.groupdict())  # 键值映射（命名才会有）
# ret = re.findall(r"(?P<sanle>\d{3})\s+(\w+)",msg)
# print(ret)
#
# ######## 引用分组

# #########正则标记
# msg ="""
# python
# PYTHON
# """
#
# # 对大小写不敏感的标志位
# ret = re.findall(r"python",msg)
# print(ret)
# ret = re.findall(r"python",msg,re.I)# 不区分大小写，大小写不敏感标志位
# print(ret)
# # 多行匹配和大小写不敏感
# # 多行匹配主要是影响开头结尾（^和$)
# ret = re.findall(r"^python$",msg,re.I)
# print(ret)
# ret = re.findall(r"python",msg,re.I|re.M)
# print(ret)
# # . 除换行符以外的任意字符,遇到换行符就停止匹配，进行下一行的匹配
# ret = re.findall(r".+",msg)
# print(ret)
# # re.S 让. 表示任意字符包括换行符
# ret = re.findall(r".+",msg,re.S)
# print(ret)

# 内联标记
# 写在正则表达式里面的
#re.findall("(?im)^python$",msg)  #大小写不敏感以及多行匹配

# msg = """
# http://www.baidu.com
# https://www.baidu.com
# https://www.baidu.com?a=1
# https://www.baidu.com/a/b/c
# """
# ret = re.findall(r"(?:http://|https://)(\w+\.+\w+\.com)",msg)
# print(ret)
# ret = re.findall(r"(?:https?://)(\w+\.+\w+\.com)",msg)
# print(ret)
#
# ####### 正则断言
# # 零宽断言
# s = "a regular expression expgular reeegular"
# print(re.findall(r"re(?=gular)",s)) # 后面接gular的re
# print(re.findall(r"re(?!gular)",s)) # 后面不是gular的re
#
# print(re.findall(r"(?<=re)gular",s))
#
# s = "hello , i am python,'sanchuang tongle' learning"
# print(re.findall(r"(?<=\').+(?=\')|\w+",s))
#
# import requests
# ret = requests.get("https://www.sanchuangedu.cn/")
# ret1 =str(ret.text)
# ret2 = re.findall(r"http.*?\.jpg",ret1)
# print(ret2)
#
# subprocess.getoutput
# subprocess.call 返回值

# import  requests
# ret1 = requests.get("https://www.sanchuangedu.cn/")
# ret2 = ret1.text
# print(ret2)
# ret3 = re.findall(r"<\w>(<img scr="/.*?/.jpg">)</a>",ret2,re.I)
# print(ret3)
#
# while True:
#     num = input("请输入一个数字：")
#     ret = re.findall(r"^\+?\d+$",num)
#     ret1 = re.findall(r"^\-\d+$",num)
#     ret2 = re.findall(r"^\+?\d+$\.\d+$",num)
#     if ret:
#         print("正整数")
#     if ret1:
#         print("负整数")
#     if ret2:
#         print("正负数")

# ########贪婪与非贪婪
# msg = "cats and dogs, cats1 and dogs1"
# # 贪婪  尽可能的匹配长的字符串
# print(re.findall(r"cat.*s",msg))
# # 非贪婪  在通配符的后面加一个？，匹配到了之后就退出
# print(re.findall(r"cat.*?s",msg))


# msg = "yyy@126.comaaa@bb.comcom@126.comdd@qq.comxxxxxxxx@163.com"
# ret = re.findall(r"(?:com)*([1-9a-z]{2,}@(?:qq|163|126)\.com)",msg)
# print(ret)
#
#
# msg = """
# http://hu.www.baidu.com
# https://www.baidu.com
# https://www.baidu.com/xxx
# https://www.baidu.com?
# """
# ret = re.findall(r"(https?://(?:\w+\.)+\w{2,}\.com)",msg)
# print(ret)
#
# msg = """
# hjh7687ew
# 92r222e
# 2,rfwyg123
# 4546798rw
# kjhhf3
# g9876_rwwr
# """
# ret = re.findall(r"((?:[a-z]+)(?:[0-9a-z_]+){8,10})",msg)
# print(ret)
#
# # msg = "www.lilu.com"
# # ret = re.findall(r"([a-z]+\.[a-z]+\.com)",msg)
# # print(ret)
# # ret1 = re.findall(r"((?:[a-z]+\.){2}com)",msg)
# # print(ret1)
#
# msg2 = "<html>test1<div>test2</div><div><a href =/lianxi/>test3</a>test4</div></html>"
# ret1 = re.findall(r"(?:<(?:div)>(?:<[\w\= /]+?>)*)|<[/\w]*>(.*?)(?:<[/\w\= ]+?>)*<(?:/div)>",msg2)
# # ret = re.findall(r"(?:test2|test3)",msg2)
# print(ret1)
#
# msg = "i am cici, welcome to 'sc tl' ths"
# print(re.findall(r"(?<=\').+(?=\')|\w+",msg))


#<a href="https://www.ksyun.com/"><img src="/upload/links/%E9%87%91%E5%B1%B1%E4%BA%91.jpg" alt=""></a>
import  requests
photo = []
ret1 = requests.get("https://www.sanchuangedu.cn/")
ret2 = ret1.text
#print(ret2)
ret3 = re.findall(r"(?:<.*?>)*<img src\=\"(.*)\" .*?>(?:<.*?>)*",ret2,re.I)

for i in ret3:
    photo.append("https://www.sanchuangedu.cn/"+i)
print(photo)

i = 1
for j in photo:
    r = requests.get(j)
    print(f"图片{i}下载中")
    dir = "./photo/sanchuang_pic" + str(i) + ".jpg"
    fp = open(dir, 'wb')
    fp.write(r.content)
    fp.close()
    i += 1
print("图片下载成功")

